#include <bits/stdc++.h>
using namespace std;



struct ListNode {
    int val;
    ListNode *next;
    ListNode() : val(0), next(nullptr) {}
    ListNode(int x) : val(x), next(nullptr) {}
    ListNode(int x, ListNode *next) : val(x), next(next) {}
};

class Solution {
public:
    ListNode* rotateRight(ListNode* head, int k) {
        if(head == NULL || k==0) return head;
        int n = 0;
        ListNode *p = head; ListNode *rear = head;
        while(p!=NULL) {
            ++n; 
            if(p->next == NULL) rear = p; 
            p=p->next;
        }
        k = k%n;
        if(k == 0) return head;
        int m = n-k-1;
        p = head;
        for(int i=0; i<m; ++i) p = p->next;

        ListNode *newHead = p->next;
        p->next = NULL;
        rear->next = head;
        return newHead;
    }
};

int main(){

}